using NodeCanvas.DialogueTrees;
using UnityEngine;

namespace NodeCanvas.BehaviourTrees
{
	[AddComponentMenu("")]
	[Name("Dialogue")]
	[Category("Nested")]
	[Description("Will Execute the nested dialogue assigned and return Success or Failure depending on the Dialogue Tree EndState.\nBy default a Dialogue Tree ends in Success.")]
	[Icon("Dialogue")]
	public class BTNestedDLGNode : BTNodeBase, INestedNode
	{
		[SerializeField]
		private DialogueTree _nestedDLG;

		private DialogueTree nestedDLG
		{
			get
			{
				return _nestedDLG;
			}
			set
			{
				_nestedDLG = value;
				if (_nestedDLG != null)
				{
					_nestedDLG.agent = base.graphAgent;
					_nestedDLG.blackboard = base.graphBlackboard;
				}
			}
		}

		public Graph nestedGraph
		{
			get
			{
				return nestedDLG;
			}
			set
			{
				nestedDLG = (DialogueTree)value;
			}
		}

		public override string nodeName
		{
			get
			{
				return base.nodeName.ToUpper();
			}
		}

		protected override Status OnExecute(Component agent, Blackboard blackboard)
		{
			if (nestedDLG == null || nestedDLG.primeNode == null)
			{
				return Status.Failure;
			}
			if (base.status == Status.Resting || nestedDLG.isPaused)
			{
				base.status = Status.Running;
				nestedDLG.StartGraph(agent, blackboard, OnDLGFinished);
			}
			return base.status;
		}

		private void OnDLGFinished()
		{
			if (base.status == Status.Running)
			{
				base.status = (Status)nestedDLG.endState;
			}
		}

		protected override void OnReset()
		{
			if ((bool)nestedDLG)
			{
				nestedDLG.StopGraph();
			}
		}

		public override void OnGraphPaused()
		{
			if ((bool)nestedDLG)
			{
				nestedDLG.PauseGraph();
			}
		}
	}
}
